<!DOCTYPE html>
<html>

<head>
    <title>ScalarField / Mask</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <link rel="stylesheet" href="//unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
    <link rel="stylesheet" href="examples.css" />

    <link href="https://fonts.googleapis.com/css?family=Roboto:100,400" rel="stylesheet">
</head>

<body>
    <h1 class="title mapTitle">ScalarField Mask</h1>
    <div id="map"></div>

    <!-- CDN -->
    <script src="//d3js.org/d3.v4.min.js"></script>
    <script src="//npmcdn.com/leaflet@1.2.0/dist/leaflet.js"></script>
    <script src="//npmcdn.com/geotiff@0.3.6/dist/geotiff.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/chroma-js/2.1.0/chroma.min.js"></script>

    <!-- Plugin -->
    <script src="dist/leaflet.canvaslayer.field.js"></script>

    <script>
        let map = L.map('map');
        var url = 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/dark_nolabels/{z}/{x}/{y}.png';
        L.tileLayer(url).addTo(map);

        d3.text('data/Bay_Speed.asc', function (asc) {
            let s = L.ScalarField.fromASCIIGrid(asc);
            const mask = {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                -3.7772369384765625,
                                43.47808605010285
                            ],
                            [
                                -3.78204345703125,
                                43.46438246220357
                            ],
                            [
                                -3.7405014038085933,
                                43.45914936352795
                            ],
                            [
                                -3.734664916992187,
                                43.47310328673331
                            ],
                            [
                                -3.728828430175781,
                                43.48929576545247
                            ],
                            [
                                -3.743934631347656,
                                43.498262040099405
                            ],
                            [
                                -3.756637573242188,
                                43.498262040099405
                            ],
                            [
                                -3.768653869628906,
                                43.492533740417976
                            ],
                            [
                                -3.7772369384765625,
                                43.47808605010285
                            ]
                        ]
                    ]
                }
            };
            s.setSpatialMask(mask);
            let layer = L.canvasLayer.scalarField(s).addTo(map);

            map.fitBounds(layer.getBounds());
            layer.on('click', function (e) {
                if (e.value !== null) {
                    let v = e.value.toFixed(3);
                    let html = `<span class="popupText">Surface currents velocity ${v} m/s</span>`;
                    let popup = L.popup().setLatLng(e.latlng).setContent(html).openOn(map);
                }
            });
        });

    </script>
</body>

</html>